﻿using System.IO;
using UnityEditor;
using UnityEngine;

public static class FolderControlTool
{
    private static string OProjectPath = Application.dataPath + "/_OProject/DesignerProject";
    private static string OProjectMetaFile = Application.dataPath + "/_OProject/DesignerProject.meta";
    private static string OProjectMetaMoveFile = Application.dataPath + "/_OProject/.DesignerProject.meta";

    private static string GameResPath = Application.dataPath + "/_Game/Resources";
    private static string GameResMetaFile = Application.dataPath + "/_Game/Resources.meta";
    private static string GameResMetaMoveFile = Application.dataPath + "/_Game/.Resources.meta";

    [MenuItem("[FC Options]/Folder/DesignerProject/隐藏", false, 0)]
    private static void HideOProjectFolder()
    {
        SetDirHide(OProjectPath);
        MoveFile(OProjectMetaFile, OProjectMetaMoveFile);
        AssetDatabase.Refresh();
    }

    [MenuItem("[FC Options]/Folder/DesignerProject/显示", false, 1)]
    private static void ShowOProjectFolder()
    {
        SetDirNoHide(OProjectPath);
        MoveFile(OProjectMetaMoveFile, OProjectMetaFile);
        AssetDatabase.Refresh();
    }

    [MenuItem("[FC Options]/Folder/DesignerProject/隐藏", true, 0)]
    private static bool IsHideOProjectFolder()
    {
        DirectoryInfo dirInfo = new DirectoryInfo(OProjectPath);
        return !HasThisAttribute(dirInfo.Attributes, FileAttributes.Hidden);
    }

    [MenuItem("[FC Options]/Folder/DesignerProject/显示", true, 1)]
    private static bool IsShowOProjectFolder()
    {
        DirectoryInfo dirInfo = new DirectoryInfo(OProjectPath);
        return HasThisAttribute(dirInfo.Attributes, FileAttributes.Hidden);
    }

    [MenuItem("[FC Options]/Folder/GameResources/隐藏", false, 2)]
    public static void HideGameResFolder()
    {
        SetDirHide(GameResPath);
        MoveFile(GameResMetaFile, GameResMetaMoveFile);
        AssetDatabase.Refresh();
    }

    [MenuItem("[FC Options]/Folder/GameResources/显示", false, 3)]
    public static void ShowGameResFolder()
    {
        SetDirNoHide(GameResPath);
        MoveFile(GameResMetaMoveFile, GameResMetaFile);
        AssetDatabase.Refresh();
    }

    [MenuItem("[FC Options]/Folder/GameResources/隐藏", true, 2)]
    private static bool IsHideGameResFolder()
    {
        DirectoryInfo dirInfo = new DirectoryInfo(GameResPath);
        return !HasThisAttribute(dirInfo.Attributes, FileAttributes.Hidden);
    }

    [MenuItem("[FC Options]/Folder/GameResources/显示", true, 3)]
    private static bool IsShowGameResFolder()
    {
        DirectoryInfo dirInfo = new DirectoryInfo(GameResPath);
        return HasThisAttribute(dirInfo.Attributes, FileAttributes.Hidden);
    }

    private static bool HasThisAttribute(FileAttributes fileAttributes, FileAttributes attribute)
    {
        return ((fileAttributes & attribute) != 0);
    }

    private static void SetDirHide(string dir)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(dir);
        dirInfo.Attributes |= FileAttributes.Hidden;
    }

    private static void SetDirNoHide(string dir)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(dir);
        dirInfo.Attributes &= ~FileAttributes.Hidden;
    }

    private static void MoveFile(string formFile, string toFile)
    {
        if (File.Exists(formFile))
        {
            if (File.Exists(toFile)) File.Delete(toFile);
            File.Move(formFile, toFile);
        }
    }
}